import base64

import requests


# http://likede2-partner.itheima.net/#/login

def graphic_verification_code(img_name):
    # 申请的key
    api_key = 'Bps3ecwlbmbEP0qoU8F9YBlA'
    secret_key = 'SFY8RsSyDKzCcsBYqN2QvvrAs5zNCc2C'
    host = f'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_' \
           f'credentials&client_id={api_key}&client_secret={secret_key}'
    request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
    f = open(img_name, 'rb')  # 二进制方式打开图片文件
    img = base64.b64encode(f.read())
    params = {"image": img}
    access_token = requests.get(host).json()['access_token']
    request_url = request_url + "?access_token=" + access_token
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    response = requests.post(request_url, data=params, headers=headers)
    if response:
        print(response.json()['words_result'][0])


if __name__ == '__main__':
    graphic_verification_code('yzm.png')
